package com.qingke.oms.command.system;

import org.apache.commons.codec.digest.DigestUtils;

import com.qingke.oms.app.OrderManagementSystemAppliction;
import com.qingke.oms.command.SystemCommand;
import com.qingke.oms.pojo.User;
import com.qingke.oms.util.OMSConsole;

public class LoginCommand extends SystemCommand{

	@Override
	public void execute() {
		OMSConsole.println();
		OMSConsole.println("----------------");
		String username=OMSConsole.promptNotEmpty("username:");
		String password=OMSConsole.promptNotEmpty("password:");
		OMSConsole.println("----------------");
		
		if(!dao.isUsernameExists(username)){
			OMSConsole.println("The user does not exist");
			return ;
		}
		
		User user=dao.login(username, DigestUtils.md5Hex(password));
		
		if(user!=null){
			OrderManagementSystemAppliction.getInstance().setUser(user);
			OMSConsole.println("Login success.");
			OMSConsole.println();
		}
		else{
			OMSConsole.println("Login failed , password is not correct");
			OMSConsole.println();
		}
	}

}
